<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>在文本中寻找 URI 地址</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="ShareURL0602.html">Share the URLs (February 2006)</a>&nbsp;&nbsp;>>Next: <a href="Catalyst_Authentication.html">Catalyst 的用户鉴定登陆</a></p>
<h1>在文本中寻找 URI 地址</h1>
<div class='content'>
<p>Category: <a href='Modules.html'>Modules</a> &nbsp; Keywords: <b>uri find</b></p>我们经常碰到这样的任务，在一段文本中找到 URL/URI 地址，然后让这个地址变为可以点击。<br />最寻常的方法就是用正则表达式来做。不过有时候这不是很理想。<p />CPAN 中有一个 <a href="http://search.cpan.org/perldoc?URI::Find">URI::Find</a> 模块专门来做这事。实例代码如下：<br /><pre>#!/usr/bin/perl<br />use strict;<br />use warnings;<p />use URI::Find;<p />my $text = &lt;&lt;HTML;<br />hi, it's from http://www.fayland.org/ then 1313s.com?<br />HTML<p />my $finder = URI::Find->new(<br /> &nbsp; &nbsp;sub {<br /> &nbsp; &nbsp; &nbsp; &nbsp;my($uri, $orig_uri) = @_;<br /> &nbsp; &nbsp; &nbsp; &nbsp;return qq|&lt;a href="$uri">$orig_uri&lt;/a>|;<br /> &nbsp; &nbsp;});<br />$finder->find(\$text);<p />print $text;</pre>这样 $text 的输出会变为：<br /><pre>hi, it's from &lt;a href="http://www.fayland.org/">http://www.fayland.org/&lt;/a> then<br /> 1313s.com?</pre>如果想让 1313s.com 也变为可点击，则可以使用 <a href="http://search.cpan.org/perldoc?URI::Find::Schemeless">URI::Find::Schemeless</a><p /><pre>#!/usr/bin/perl<br />use strict;<br />use warnings;<p />use URI::Find::Schemeless;<p />my $text = &lt;&lt;HTML;<br />hi, it's from http://www.fayland.org/ then 1313s.com?<br />HTML<p />my $finder = URI::Find::Schemeless->new(<br /> &nbsp; &nbsp;sub {<br /> &nbsp; &nbsp; &nbsp; &nbsp;my($uri, $orig_uri) = @_;<br /> &nbsp; &nbsp; &nbsp; &nbsp;return qq|&lt;a href="$uri">$orig_uri&lt;/a>|;<br /> &nbsp; &nbsp;});<br />$finder->find(\$text);<p />print $text;</pre>Schemeless 的意思就是没有协议头（如 http ）也匹配。<br />$text 变为：<br /><pre>hi, it's from &lt;a href="http://www.fayland.org/">http://www.fayland.org/&lt;/a> the<br /> &lt;a href="http://1313s.com/">1313s.com&lt;/a>?</pre>大致如是。详细的查阅 <a href="http://search.cpan.org/perldoc?URI::Find">URI::Find</a> , have fun!</div>
<p><&lt;Previous: <a href="ShareURL0602.html">Share the URLs (February 2006)</a>&nbsp;&nbsp;>>Next: <a href="Catalyst_Authentication.html">Catalyst 的用户鉴定登陆</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E5%9C%A8%E6%96%87%E6%9C%AC%E4%B8%AD%E5%AF%BB%E6%89%BE%20URI%20%E5%9C%B0%E5%9D%80&url=http://www.fayland.org/journal/URI_Find.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='wwwsf4.cn.html'>WWW Security FAQ: CGI Scripts</a> < <span class='digit'>2004-11-10 22:30:44</span> ></li><li><a href='mod_perl_configuring.html'>mod_perl 配置的一些指令</a> < <span class='digit'>2005-11-13 11:38:05</span> ></li></ul>
Created on <span class="digit">2006-02-20 16:41:30</span>, Last modified on <span class="digit">2006-02-20 16:43:26</span><br />
Copyright 2004-2006 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.65.
</body>
</html>